<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${config.mapperPackage}.${javaDomainName}${config.mapperNameSuffix}">
  <resultMap id="BaseResultMap" type="${config.domainPackage}.${javaDomainName}">
<#if columns?exists>
  <#list columns as column>
    <#if column.columnName == "id">
    <id column="${column.columnName}" jdbcType="${column.javaFieldType.jdbcType}" property="${column.javaFieldName}" />
    </#if>
    <#if column.columnName != "id">
    <result column="${column.columnName}" jdbcType="${column.javaFieldType.jdbcType}" property="${column.javaFieldName}" />
    </#if>
  </#list>
</#if>
  </resultMap>
  <sql id="BaseColumns">
<#if columns?exists>
  <#list columns as column>
    ${"$"}{alias}.${column.columnName}<#if !column?is_last>,</#if>
  </#list>
</#if>
  </sql>

  <sql id="BaseWhere">
<#if columns?exists>
  <#list columns as column>
    <#if column.columnName == "is_deleted"><#if column?index != 0>AND</#if> ${"$"}{alias}.is_deleted = 0</#if><#if column.columnName != "is_deleted"><if test="${column.javaFieldName} != null"><#if column?index != 0>AND</#if> ${"$"}{alias}.${column.columnName} = #${"{"}${column.javaFieldName},jdbcType=${column.javaFieldType.jdbcType}${"}"}</if></#if>
  </#list>
</#if>
  </sql>

  <delete id="deleteWithLogic" parameterType="java.lang.Long">
      update ${tableName}
      set is_deleted = 1
      where id = #${"{"}id,jdbcType=BIGINT${"}"} AND is_deleted = 0
  </delete>

  <insert id="insert" parameterType="${config.domainPackage}.${javaDomainName}">
      insert into ${tableName} (
      <#list columns as column>
        ${column.columnName}<#if !column?is_last>,</#if>
      </#list>
      )
      values (
      <#list columns as column>
      <#if column.columnName = "is_deleted">
        0<#if !column?is_last>,</#if>
      </#if>
      <#if column.columnName != "is_deleted">
        #${"{"}${column.javaFieldName},jdbcType=${column.javaFieldType.jdbcType}${"}"}<#if !column?is_last>,</#if>
      </#if>
      </#list>
      )
  </insert>

  <update id="updateByPrimaryKey" parameterType="${config.domainPackage}.${javaDomainName}">
      update ${tableName}
      set
  <#list columns as column>
    <#if column.columnName != "id" && column.columnName != "is_deleted" && column.columnName != "version">
      ${column.columnName} = #${"{"}${column.javaFieldName},jdbcType=${column.javaFieldType.jdbcType}${"}"}<#if !column?is_last>,</#if>
    </#if>
    <#if column.columnName == "version">
      ${column.columnName} = ${column.javaFieldName}${"+1"}<#if !column?is_last>,</#if>
    </#if>
  </#list>
      where id = #${"{"}id,jdbcType=BIGINT${"}"}
      and is_deleted = 0
  </update>

  <update id="updateWithOptionalByPrimaryKey" parameterType="${config.domainPackage}.${javaDomainName}">
      update ${tableName}
      <set>
      <#list columns as column>
        <#if column.columnName != "id" && column.columnName != "is_deleted" && column.columnName != "version">
          <if test="${column.javaFieldName} != null">${column.columnName} = #${"{"}${column.javaFieldName},jdbcType=${column.javaFieldType.jdbcType}${"}"}<#if !column?is_last>,</#if></if>
        </#if>
        <#if column.columnName == "version">
          ${column.columnName} = ${column.javaFieldName}${"+1"}<#if !column?is_last>,</#if>
        </#if>
      </#list>
      </set>
      where id = #${"{"}id,jdbcType=BIGINT${"}"}
      and is_deleted = 0
  </update>

  <select id="select" parameterType="${config.domainPackage}.${javaDomainName}" resultMap="BaseResultMap">
      select
      <include refid="BaseColumns">
          <property name="alias" value="T"/>
      </include>
      from ${tableName} T
      <where>
          <include refid="BaseWhere">
              <property name="alias" value="T"/>
          </include>
      </where>
  </select>

  <select id="selectAll" parameterType="${config.domainPackage}.${javaDomainName}" resultMap="BaseResultMap">
      select
      <include refid="BaseColumns">
          <property name="alias" value="T"/>
      </include>
      from ${tableName} T
      <where>
          <include refid="BaseWhere">
              <property name="alias" value="T"/>
          </include>
      </where>
  </select>
</mapper>